<?php
/**
 * Logger Action Helper
 * This action helper makes it possible to log events that happen in the app.
 *
 * @package     Visi
 * @subpackage  Action Helpers
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2010 Luke Visinoni <luke.visinoni@gmail.com>
 * @version     $Id$
 * @todo        I'm not in love with this design. It forces the controllers to
 *              make assumptions they shouldn't have to make. Fix that.
 */
class Visi_Controller_Action_Helper_Logger extends Zend_Controller_Action_Helper_Abstract {

    protected $_loggers = array();
    
    public function __construct(Array $loggers = array()) {
    
        $this->setLoggers($this->_loggers = $loggers);
    
    }
    
    public function setLoggers(Array $loggers) {
    
        $this->_loggers = $loggers;
    
    }
    
    public function addLogger($name, Zend_Log $logger) {
    
        $this->_loggers[$name] = $logger;
    
    }
    
    public function direct($message, $logger = null, $level = null) {
    
        if (is_null($logger)) {
            $logger = 'default';
        }
        if (is_null($level)) {
            $level = Zend_Log::INFO;
        }
        if (!array_key_exists($logger, $this->_loggers)) {
            throw new IsItLegit_Controller_Action_Helper_Exception("'{$logger}' is not a defined logger.");
        }
        $logger = $this->_loggers[$logger];
        $logger->log($message, $level);
    
    }
    
    /**
     * On the login action, the bootstrap that sets the user id will happen
     * before the user is actually logged in. So it has to be set manually.
     */
    public function setUserId($user_id) {
    
        if (!array_key_exists('user', $this->_loggers)) {
            throw new IsItLegit_Controller_Action_Helper_Exception('There is no user logger to set an ID on.');
        }
        $this->_loggers['user']->setEventItem('user_id', $user_id);
    
    }

}